package no.ntnu.fp.gui;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;

import no.ntnu.fp.client.CalendarClient;
import no.ntnu.fp.client.LoginFailedException;

import javax.swing.JTextField;
import javax.swing.JLabel;

public class LoginGui extends javax.swing.JFrame {

	private static final long serialVersionUID = -4633600957326465989L;
	
	/** Creates new form login2 */
    public LoginGui() {
        initComponents();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        usernameField = new javax.swing.JTextField();
        usernameField.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		loginButton.doClick();
        	}
        });
        loginButton = new javax.swing.JButton();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        portNumber = new JTextField();
        portNumber.setText("5555");
        portNumber.setColumns(10);        
        hostName = new JTextField();
        hostName.setText("127.0.1.1");
        hostName.setColumns(10);

        setBounds(0, 0, 253, 207);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        usernameField.setText("");

        loginButton.setText("Log In");
        loginButton.setMnemonic(KeyEvent.VK_ENTER);
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
                
        		//Open the calendar window
                EventQueue.invokeLater(new Runnable() {
        	        public void run() {
        	        	
                        //Send login data
        	        	String password = Integer.toString(new String( passwordField.getPassword() ).hashCode());
        	        	try {
        	        		new CalendarClient(usernameField.getText(), password, hostName.getText(), Integer.parseInt(portNumber.getText()));
        	            	LoginGui.this.dispose();
        	        	}
        	        	catch (LoginFailedException e) {
        	    			JOptionPane.showMessageDialog(LoginGui.this, "Invalid username or password!");        	        		
						}
        	        }
                });
            }
        });

        usernameLabel.setText("Username:");

        passwordLabel.setText("Password:");

        passwordField.setText("");
        passwordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        
        JButton button = new JButton("New User");
        button.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		//TODO: implement this
        		JOptionPane.showMessageDialog(LoginGui.this, "Not implemented!");
        	}
        });
                
        JLabel lblHostnameOrIp = new JLabel("Hostname or IP:");
        
        JLabel lblPortNumber = new JLabel("Port number:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(22)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(lblHostnameOrIp))
        						.addComponent(passwordLabel)
        						.addComponent(usernameLabel))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(hostName, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(lblPortNumber)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(portNumber, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
        						.addComponent(usernameField)
        						.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
        				.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        					.addComponent(loginButton, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        					.addComponent(button, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)))
        			.addGap(101))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(usernameLabel)
        				.addComponent(usernameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(passwordLabel)
        				.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(hostName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblHostnameOrIp)
        				.addComponent(lblPortNumber)
        				.addComponent(portNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addComponent(loginButton)
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addComponent(button)
        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        getContentPane().setLayout(layout);

        pack();
    }// </editor-fold>

    
    
    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
    	loginButton.doClick();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    // Variables declaration - do not modify
    private javax.swing.JButton loginButton;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField usernameField;
    private JTextField portNumber;
    private JTextField hostName;
}